/**
 * Filename: d:\CODE\FTP\FTP_TEST\FTP\static\js\login.js
 * Path: d:\CODE\FTP\FTP_TEST\FTP\static\js
 * Created Date: Tuesday, May 10th 2022, 9:35:59 am
 * Author: xiguan
 * 
 * Copyright (c) 2022 XiGuan Inc.
 */


const basic_url = "http://127.0.0.1:8880";

// 封装发送请求
function ajax(params){
    let url = basic_url+params.url;
    let data = params.data;
    let method = "GET";
    if(params.method !== ""){
        method = params.method;
    }

    return $.ajax({
        url : url,
        data : data,
        method: method
    });
}
// 监听
$(function(){
    // 当用户点击了登陆按钮
    $("#login-btn").click(() => {
        let username = $("#username").val();
        let passwd = $("#password").val();

        console.info(username, "haha", passwd);

        if(username === "" || passwd === ""){
            alert("提示: 用户名和密码不能为空!");
        }
        else{
            let params = {
                url : "/user/login_do",
                data:{
                    username : username,
                    passwd : passwd
                },
                method : "POST"
            };
        
            ajax(params).then(res => {
                console.info("status: ", res.status);
                if(res.status === "OK"){
                    //alert("提示: 用户名或者密码输入错误!");
                    console.info("进入index1");
                    window.location.href="/user/go_index/";
                    console.info("进入index2");
                }
                else{
                    alert("提示: 用户名或者密码输入错误!");
                }
            });
        }
    });
    // 用户名输入框聚焦
    $("#username").focus(() => {
        $("#tip").hide();
    });

    // 密码输入框聚焦
    $("#password").focus(() => {
        $("#tip").hide();
    });


    // 用户点击注册
    $("#register-text").click(() => {
        window.location.href="/user/go_register";
    });

})